package com.geoboard.entity;

import java.util.Date;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;

import android.os.Parcel;
import android.os.Parcelable;

import com.google.android.gms.maps.model.GroundOverlay;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Polygon;

@JsonIgnoreProperties({"polygon", "overlay"})
public class Board implements Parcelable {
    private Long id;
    private String name;
    private String userName;
    private Date timestamp;
    @JsonProperty("nlat")
    private double north;
    @JsonProperty("wlng")
    private double west;
    @JsonProperty("slat")
    private double south;
    @JsonProperty("elng")
    private double east;
    private int zoom;
    private Polygon polygon;
    private GroundOverlay overlay;
    
    public static final Parcelable.Creator<Board> CREATOR 
    	= new Parcelable.Creator<Board>() {

			@Override
			public Board createFromParcel(Parcel in) {
				return new Board(in);
			}

			@Override
			public Board[] newArray(int size) {
				return new Board[size];
			}
		};

    public Board() {
    }
    
    public Board(Parcel in) {
    	id = in.readLong();
    	name = in.readString();
    	userName = in.readString();
    	timestamp = (Date) in.readSerializable();
    	north = in.readDouble();
    	west = in.readDouble();
    	south = in.readDouble();
    	east = in.readDouble();
    	zoom = in.readInt();
    }
    
    public Board(String name, String username) {
    	this.name = name;
    	this.userName = username;
    }

    public Board(String name, double north, double west, 
            double south, double east, int zoom) {
        this.name = name;
        this.north = north;
        this.west = west;
        this.south = south;
        this.east = east;
        this.zoom = zoom;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Date getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }

    public double getNorth() {
        return north;
    }

    public void setNorth(double north) {
        this.north = north;
    }

    public double getWest() {
        return west;
    }

    public void setWest(double west) {
        this.west = west;
    }

    public double getSouth() {
        return south;
    }

    public void setSouth(double south) {
        this.south = south;
    }

    public double getEast() {
        return east;
    }

    public void setEast(double east) {
        this.east = east;
    }
    
    public int getZoom() {
    	return zoom;
    }
    
    public void setZoom(int zoom) {
    	this.zoom = zoom;
    }
    
    public Polygon getPolygon () {
    	return polygon;
    }
    
    public void setPolygon (Polygon polygon) {
    	this.polygon = polygon;
    }
    
    public GroundOverlay getOverlay(){
    	return overlay;
    }
    
    public void setOverlay(GroundOverlay goo){
    	overlay = goo;
    }
    
    public boolean containsPoint(LatLng point) {
    	return (point.latitude < this.north && point.latitude > this.south
    			&& point.longitude > this.west && point.longitude < this.east);
    }
    
    public boolean intersectsRect(LatLngBounds rect) {
    	LatLng ne = rect.northeast;
    	LatLng sw = rect.southwest;
    	return (sw.latitude < this.north && ne.longitude > this.west
    			&& ne.latitude > this.south && sw.longitude < this.east);
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Board)) {
            return false;
        }
        Board other = (Board) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return getName();
    }

	@Override
	public void writeToParcel(Parcel out, int flags) {
		out.writeLong(id);
		out.writeString(userName);
		out.writeSerializable(timestamp);
		out.writeDouble(north);
		out.writeDouble(west);
		out.writeDouble(south);
		out.writeDouble(east);
		out.writeInt(zoom);
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}    
}
